﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace PWAsker
{
    //TODO: REQUIREMENT: The Executables need to get objects to enable the following three features
        //TODO: FEATURE: End Application after sucessfull call (on/offable)
        //TODO: FEATURE: Add Profiles, Profiles are chosen through the PC-Name you are on
        //TODO: FEATURE: Add feature to wait until command completes or x seconds

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public const String PW_REGEX = "%PW%";
        public const String CONFIGFILE = "settings.xml";

        Configuration config;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void buttonOK_Click(object sender, RoutedEventArgs eventArgs)
        {

            if (config == null)
            {
                Debug.WriteLine("Config file not loaded. Attemting to load it now");
                config = Configuration.readConfiguration(CONFIGFILE);
            }

            //Executing commands, if config was load
            if (config != null)
            {
                List<Executable> lSzExecutable = config.getCurrentExecutables();

                for (int i = 0; i < lSzExecutable.Count; i++)
                {
                    //Executing executables
                    lSzExecutable.ElementAt(i).execute(passwordField.Password, PW_REGEX);
                }
            }

            passwordField.Clear();
        }

        private void buttonConfig_Click(object sender, RoutedEventArgs eventArgs)
        {
            
            if (config == null)
            {
                Debug.WriteLine("Config file not loaded. Attemting to load it now");
                config = Configuration.readConfiguration(CONFIGFILE);
            }


            //Starting configuration-dialog, only if executables is not empty e.g. the setting file was read
            if (config != null)
            {
                List<Executable> lSzCommands = new List<Executable>(config.getCurrentExecutables());

                Debug.WriteLine("Starting configuration window");

                Debug.WriteLine("executables before config");
                debugDumpList(lSzCommands);

                ConfigurationWnd confWnd = new ConfigurationWnd(lSzCommands);
                confWnd.Owner = this;
                confWnd.ShowDialog();

                Debug.WriteLine("executables after config");
                debugDumpList(lSzCommands);

                if (confWnd.madeChanges())
                {
                    Debug.WriteLine("configuration changed. Rewriting xml-file");
                    
                    //deleting old executables
                    config.clearExecutables();

                    //adding new executables
                    for (int i = 0; i < lSzCommands.Count; i++)
                    {
                        config.addExecutable(new Executable(lSzCommands.ElementAt(i).ExecCommand, lSzCommands.ElementAt(i).WaitForCommand, lSzCommands.ElementAt(i).WaitSecs));
                    }

                    Debug.WriteLine("The configuration contains the following Executables (full dump):");
                    debugDumpList(config.getCurrentExecutables());

                    Configuration.writeConfiguration(config,CONFIGFILE);
                }
                else
                {
                    Debug.WriteLine("No changes in cofiguration detected. No need for rewriting xml-file");
                }
            }

            
        }

        private void debugDumpList(List<Executable> ExecList)
        {
            for (int i = 0; i < ExecList.Count; i++)
            {
                Debug.WriteLine(ExecList.ElementAt(i).ExecCommand + " - " + ExecList.ElementAt(i).WaitForCommand + " - " + ExecList.ElementAt(i).WaitSecs);
            }

            Debug.WriteLine("------------------------");
        }
    }
}
